//
//  DockViewController.m
//  McRemote
//
//  Created by Magic on 1/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DockViewController.h"
#import "DockManager.h"
#import "MRMessageCenter.h"

@interface DockViewController () 

@property (nonatomic, weak) GridView *gridView;
- (void)refreshWithNotification:(NSNotification *)notification;
- (void)refreshData;

@end

@implementation DockViewController

@synthesize gridView = _gridView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)awakeFromNib
{
    self.navigationItem.title = NSLocalizedString(self.title, @"");
    self.navigationController.tabBarItem.title = NSLocalizedString(self.title, @"");
}

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 */
- (void)loadView
{
    NSString *bgPath = [[NSBundle mainBundle] pathForResource:@"bg" ofType:@"png"];
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:bgPath]];
    bgView.userInteractionEnabled = YES;
    
    GridView *gridView = [[GridView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    gridView.gridViewDataSource = self;
    gridView.gridViewDelegate = self;
    
    [bgView addSubview:gridView];
    
    gridView.scrollIndicatorInsets = UIEdgeInsetsMake(8, 0, 0, 10);
    gridView.backgroundColor = [UIColor clearColor];
    gridView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
    self.view = bgView;
    self.gridView = gridView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshWithNotification:) name:@"LaunchApplication" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshWithNotification:) name:@"ApplicationIcon" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshWithNotification:) name:@"TerminateApplication" object:nil];
    
    [self refreshData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
 */

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)refresh
{
    [self refreshData];
}

#pragma mark - GridViewDataSource Protocol implementation

- (GridViewCell *)gridView:(GridView *)gridView cellAtIndex:(NSInteger)index
{
    if (index >= [[DockManager sharedManager] numberOfDockApp]) {
        return nil;
    }
    
    NSString *name = [[DockManager sharedManager] dockAppNameAtIndex:index];
    
    if (name) {
        GridViewCell *cell = [GridViewCell emptyGridCell];
        cell.titleLabel.text = name;
        UIImage *icon = [[DockManager sharedManager] dockAppIconAtIndex:index];
        [cell.iconButton setImage:icon forState:UIControlStateNormal];
        
        return cell;
    }
    else return nil;
}

- (NSInteger)numberOfPagesInGridView:(GridView *)gridView
{
    int count = [[DockManager sharedManager] numberOfDockApp];
    
    if (count%9 == 0) {
        return count/9;
    }
    else return (count/9+1);
}

#pragma mark - GridViewDelegate Protocol implementation

- (void)gridView:(GridView *)gridView tapOnCell:(GridViewCell *)cell
{
    NSInteger index = [gridView indexForCell:cell];
    NSString *name = [[DockManager sharedManager] dockAppNameAtIndex:index];
    [[MRMessageCenter sharedCenter] sendLaunchAppNamed:name];
}

#pragma mark - util

- (void)refreshWithNotification:(NSNotification *)notification
{
    [self refreshData];
}

- (void)refreshData
{
    CGPoint contentOffset = self.gridView.contentOffset;
    
    [self.gridView reloadData];
    
    self.gridView.contentOffset = contentOffset;
}


@end
